package basic.courseLearn11.problem;

import org.junit.Test;

// 字符串匹配的暴力解法
public class StringMatchingViolence {
    public int findStringIndex(String arr, String str) {
        //1. 定义变量，记录匹配的字符串的下标
        int i=0,j=0;
        //2. 遍历数组，逐个元素与目标字符串进行匹配
        while(i<arr.length() && j<str.length()){
            if(arr.charAt(i)==str.charAt(j)){
                i++;
                j++;
            }else{
                i=i-j+1;
                j=0;
            }
        }
        //3. 返回
        if(j==str.length()){
            return i-j;
        }
        return -1;
    }

    @Test
    public void test(){
        String arr = "abcabbcd";
        String str = "abb";
        int index = findStringIndex(arr, str);
        System.out.println("my answer is " + index);
        System.out.println("right answer is "+ arr.indexOf(str));
    }
}
